package bingosoft.metro.datasync.uuap.helper;

import bingosoft.metro.datasync.uuap.model.*;
import leap.core.AppContext;
import leap.orm.dao.Dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SyncDataHelper {

	public static final String TenantCode = AppContext.config().getProperty("uuap.tenantCode", "test");
	public static final String AppCode = AppContext.config().getProperty("uuap.appCode", "test_app_code");
	public static final String Status = "enabled";
	
	/**
	 * 删除目标菜单数据
	 */
	public static void deleteTargetMenu() {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("tenantCode", TenantCode);
		
		String sql = "DELETE FROM sys_app_menu WHERE tenant_code = #tenantCode#";
		Dao.get().executeUpdate(sql, params);
	}

	/**
	 * 构建菜单
	 * @param i
	 * @return
	 */
	public static SyncAppMenuModel buildSyncMenuModel(int i) {
		SyncAppMenuModel menu = new SyncAppMenuModel();
		menu.setSyncId("SyncId" + i);
		menu.setSyncTime(new Date());
		menu.setIsSync(true);
		
		menu.setMenuId("MenuId" + i);
		//menu.setParentMenuId(null);
		menu.setMenuCode("MenuCode" + i);
		menu.setMenuName("MenuName" + i);
		menu.setMenuUrl("MenuUrl" + i);
		menu.setMenuIcon("MenuIcon" + i);
		menu.setWbsCode("WbsCode" + i);
		menu.setWbsLevel(i);
		menu.setOrderNum(i);
		menu.setStatus(Status);
		menu.setRemark("Remark" + i);
		menu.setCreateUserId("CreateUserId" + i);
		menu.setCreateTime(new Date());
		menu.setUpdateUserId("UpdateUserId" + i);
		menu.setUpdateTime(new Date());
		menu.setTenantCode(TenantCode);

		return menu;
	}

	/**
	 * 删除目标权限数据
	 */
	public static void deleteTargetPermission() {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("tenantCode", TenantCode);
		
		String sql1 = "DELETE FROM sec_permission_rule WHERE tenant_code = #tenantCode#";
		String sql2 = "DELETE FROM sec_permission WHERE tenant_code = #tenantCode#";

		Dao.get().executeUpdate(sql1, params);
		Dao.get().executeUpdate(sql2, params);
	}
	
	/**
	 * 构建权限
	 * @param i
	 * @return
	 */
	public static SyncPermissionModel buildSyncPermissionModel(int i) {
		SyncPermissionModel p = new SyncPermissionModel();
		p.setSyncId("SyncId" + i);
		p.setSyncTime(new Date());
		p.setIsSync(true);
		
		p.setPermissionId("PermissionId" + i);
		//p.setParentPermissionId("ParentPermissionId" + i);
		p.setPermissionName("PermissionName" + i);
		p.setPermissionCode("PermissionCode" + i);
		p.setPermissionType("PermissionType" + i);
		p.setPageUrl("PageUrl" + i);
		p.setOrderNum(i);
		p.setIsDelete(false);
		p.setCreateTime(new Date());
		p.setCreateUserId("CreateUserId" + i);
		p.setUpdateTime(new Date());
		p.setUpdateUserId("UpdateUserId" + i);
		p.setTenantCode(TenantCode);
		p.setColumn1("Column1" + i);
		p.setColumn2("Column2" + i);
		p.setColumn3("Column3" + i);
		p.setColumn4("Column4" + i);
		p.setColumn5("Column5" + i);

		return p;
	}
	
	/**
	 * 构建权限规则
	 * @param i
	 * @return
	 */
	public static SyncPermissionRuleModel buildSyncPermissionRuleModel(SyncPermissionModel permission, int i) {
		SyncPermissionRuleModel rule = new SyncPermissionRuleModel();
		rule.setSyncId("SyncId" + i);
		rule.setSyncTime(new Date());
		rule.setIsSync(true);
		
		rule.setPermissionRuleId("Remark" + i);
		rule.setPermissionId(permission.getPermissionId());
		rule.setRuleName("RuleName" + i);
		rule.setRuleContent("RuleContent" + i);
		rule.setPriority(i);
		rule.setStatus(Status);
		rule.setRemark("Remark" + i);
		rule.setCreateTime(new Date());
		rule.setCreateUserId("CreateUserId" + i);
		rule.setUpdateTime(new Date());
		rule.setUpdateUserId("UpdateUserId" + i);
		rule.setTenantCode(TenantCode);

		return rule;
	}
	
	/**
	 * 删除目标角色数据
	 */
	public static void deleteTargetRole() {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("tenantCode", TenantCode);
		
		String sql1 = "DELETE FROM sec_role_permission_ref WHERE tenant_code = #tenantCode#";
		String sql2 = "DELETE FROM sec_role WHERE tenant_code = #tenantCode#";

		Dao.get().executeUpdate(sql1, params);
		Dao.get().executeUpdate(sql2, params);
	}
	
	/**
	 * 构建【SEC】角色
	 * @param i
	 * @return
	 */
	public static SyncRoleModel buildSyncRoleModel(int i) {
		SyncRoleModel role = new SyncRoleModel();
		role.setSyncId("SyncId" + i);
		role.setSyncTime(new Date());
		role.setIsSync(true);
		
		role.setRoleId("RoleId" + i);
		role.setRoleName("RoleName" + i);
		role.setRoleCode("RoleCode" + i);
		role.setRoleType("RoleType" + i);
		role.setStatus(Status);
		role.setIsDelete(false);
		role.setRemark("Remark" + i);
		role.setCreateTime(new Date());
		role.setCreateUserId("CreateUserId" + i);
		role.setUpdateTime(new Date());
		role.setUpdateUserId("UpdateUserId" + i);
		role.setTenantCode(TenantCode);
		role.setColumn1("Column1" + i);
		role.setColumn2("Column2" + i);
		role.setColumn3("Column3" + i);
		role.setColumn4("Column4" + i);
		role.setColumn5("Column5" + i);

		return role;
	}
	
	/**
	 * 构建角色权限关系
	 * @param i
	 * @return
	 */
	public static SyncRolePermissionRefModel buildSyncRolePermissionRefModel(SyncRoleModel role, SyncPermissionModel permission, int i) {
		SyncRolePermissionRefModel ref = new SyncRolePermissionRefModel();
		ref.setSyncId("SyncId" + i);
		ref.setSyncTime(new Date());
		ref.setIsSync(true);
		
		ref.setRolePermissionId("RolePermissionId" + i);
		ref.setRoleId(role.getRoleId());
		ref.setPermissionId(permission.getPermissionId());
		ref.setPermissionRuleId("PermissionRuleId" + i);
		ref.setCreateTime(new Date());
		ref.setCreateUserId("CreateUserId" + i);
		ref.setUpdateTime(new Date());
		ref.setUpdateUserId("UpdateUserId" + i);
		ref.setTenantCode(TenantCode);

		return ref;
	}
	
	/**
	 * 删除目标组织数据
	 */
	public static void deleteTargetOrg() {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("tenantCode", TenantCode);
		
		String sql1 = "DELETE FROM sec_org_role_ref WHERE tenant_code = #tenantCode#";
		String sql2 = "DELETE FROM sec_org WHERE tenant_code = #tenantCode#";

		//Dao.get().executeUpdate(sql1, params);
		Dao.get().executeUpdate(sql2, params);
	}
	
	/**
	 * 构建【SEC】组织
	 * @param i
	 * @return
	 */
	public static SyncOrgModel buildSyncOrgModel(int i) {		
		SyncOrgModel org = new SyncOrgModel();
		org.setSyncId("SyncId" + i);
		org.setSyncTime(new Date());
		org.setIsSync(true);
		
		org.setOrgId("OrgId" + i);
		//org.setParentOrgId("ParentOrgId" + i);
		org.setSourceOrgId("SoureOrgId" + i);
		org.setSourceParentOrgId("SoureParentOrgId" + i);
		org.setOrgName("OrgName" + i);
		org.setOrgFullName("FullOrgName" + i);
		org.setRegion("Region" + i);
		org.setStreet("Street" + i);
		org.setPost("Post" + i);
		org.setOrgType("OrgType" + i);
		org.setWbsCode("wbsCode" + i);
		org.setWbsLevel(i);
		org.setOrderNum(i);
		org.setStatus(Status);
		org.setIsDelete(false);
		org.setRemark("Remark" + i);
		org.setCreateTime(new Date());
		org.setCreateUserId("CreateUserId" + i);
		org.setUpdateTime(new Date());
		org.setUpdateUserId("UpdateUserId" + i);
		org.setTenantCode(TenantCode);
		org.setColumn1("Column1" + i);
		org.setColumn2("Column2" + i);
		org.setColumn3("Column3" + i);
		org.setColumn4("Column4" + i);
		org.setColumn5("Column5" + i);

		return org;
	}
	
	/**
	 * 构建【SEC】组织角色关系
	 * @param i
	 * @return
	 */
	public static SyncOrgRoleRefModel buildSyncOrgRoleRefModel(SyncOrgModel org, SyncRoleModel role, int i) {
		SyncOrgRoleRefModel ref = new SyncOrgRoleRefModel();
		ref.setSyncId("SyncId" + i);
		ref.setSyncTime(new Date());
		ref.setIsSync(true);
		
		ref.setOrgRoleId("OrgRoleId" + i);
		ref.setOrgId(org.getOrgId());
		ref.setRoleId(role.getRoleId());
		ref.setCreateTime(new Date());
		ref.setCreateUserId("CreateUserId" + i);
		ref.setUpdateTime(new Date());
		ref.setUpdateUserId("UpdateUserId" + i);
		ref.setTenantCode(TenantCode);

		return ref;
	}
	
	/**
	 * 删除目标用户数据
	 */
	public static void deleteTargetUser() {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("tenantCode", TenantCode);
		
		String sql1 = "DELETE FROM sec_org_user_ref WHERE tenant_code = #tenantCode#";
		String sql2 = "DELETE FROM sec_user_role_ref WHERE tenant_code = #tenantCode#";
		String sql3 = "DELETE FROM sec_user_org_role_ref WHERE tenant_code = #tenantCode#";
		String sql4 = "DELETE FROM sec_user WHERE tenant_code = #tenantCode#";
		String sql5 = "DELETE FROM sec_org_role WHERE tenant_code = #tenantCode#";

		Dao.get().executeUpdate(sql1, params);
		//Dao.get().executeUpdate(sql2, params);
		//Dao.get().executeUpdate(sql3, params);
		Dao.get().executeUpdate(sql4, params);
		//Dao.get().executeUpdate(sql5, params);
	}
	
	/**
	 * 构建用户
	 * @param i
	 * @return
	 */
	public static SyncUserModel buildSyncUserModel(int i) {
		
		SyncUserModel user = new SyncUserModel();
		user.setSyncId("SyncId" + i);
		user.setSyncTime(new Date());
		user.setIsSync(true);
		
		user.setUserId("UserId" + i);
		user.setSourceUserId("SourceUserId" + i);
		user.setUserType("UserType" + i);
		user.setUserName("UserName" + i);
		user.setPinyinFull("PinyinFull" + i);
		user.setPinyinInitial("PyI" + i);
		user.setAccountName("AccountName" + i);
		user.setJobNumber("JobNumber" + i);
		user.setIdentityId("IdentityId" + i);
		user.setPassword("Password" + i);
		user.setPositions("Positions" + i);
		user.setPositionType("PositionType" + i);
		user.setJobLevel("JobLevel" + i);
		user.setEmail("Email" + i);
		user.setMobile("Mobile" + i);
		user.setTelephone("Telephone" + i);
		user.setSex("Sex" + i);
		user.setBirthday(new Date());
		user.setNation("Nation" + i);
		user.setHometown("Hometown" + i);
		user.setPolitics("Politics" + i);
		user.setSchool("School" + i);
		user.setVocational("Vocational" + i);
		user.setJoinDate(new Date());
		user.setOrderNum( i);
		user.setStatus(Status);
		user.setIsDelete(false);
		user.setRemark("Remark" + i);
		user.setCreateTime(new Date());
		user.setCreateUserId("CreateUserId" + i);
		user.setUpdateTime(new Date());
		user.setUpdateUserId("UpdateUserId" + i);
		user.setTenantCode(TenantCode);
		user.setColumn1("Column1" + i);
		user.setColumn2("Column2" + i);
		user.setColumn3("Column3" + i);
		user.setColumn4("Column4" + i);
		user.setColumn5("Column5" + i);
				
		return user;
	}
	
	/**
	 * 构建组织用户关系
	 * @param i
	 * @return
	 */
	public static SyncOrgUserRefModel buildSyncOrgUserRefModel(SyncOrgModel org, SyncUserModel user, boolean isDefault, int i) {
		SyncOrgUserRefModel ref = new SyncOrgUserRefModel();
		ref.setSyncId("SyncId" + i);
		ref.setSyncTime(new Date());
		ref.setIsSync(true);
		
		ref.setOrgUserId("OrgUserId" + i);
		ref.setOrgId(org.getOrgId());
		ref.setUserId(user.getUserId());
		ref.setIsDefaultOrg(isDefault);
		ref.setOrderNum(i);
		ref.setCreateTime(new Date());
		ref.setCreateUserId("CreateUserId" + i);
		ref.setUpdateTime(new Date());
		ref.setUpdateUserId("UpdateUserId" + i);
		ref.setTenantCode(TenantCode);

		return ref;
	}
	
	/**
	 * 构建用户角色关系
	 * @param i
	 * @return
	 */
	public static SyncUserRoleRefModel buildSyncUserRoleRefModel(SyncUserModel user, SyncRoleModel role, int i) {
		SyncUserRoleRefModel ref = new SyncUserRoleRefModel();
		ref.setSyncId("SyncId" + i);
		ref.setSyncTime(new Date());
		ref.setIsSync(true);
		
		ref.setUserRoleId("UserRoleId" + i);
		ref.setUserId(user.getUserId());
		ref.setRoleId(role.getRoleId());
		ref.setOrderNum(i);
		ref.setCreateTime(new Date());
		ref.setCreateUserId("CreateUserId" + i);
		ref.setUpdateTime(new Date());
		ref.setUpdateUserId("UpdateUserId" + i);
		ref.setTenantCode(TenantCode);

		return ref;
	}
	
	/**
	 * 构建用户组织角色关系
	 * @param i
	 * @return
	 */
	public static SyncUserOrgRoleRefModel buildSyncUserOrgRoleRefModel(SyncUserModel user, SyncOrgModel org, SyncRoleModel role, int i) {
		SyncUserOrgRoleRefModel ref = new SyncUserOrgRoleRefModel();
		ref.setSyncId("SyncId" + i);
		ref.setSyncTime(new Date());
		ref.setIsSync(true);
		
		ref.setUserOrgRoleId("UserOrgRoleId" + i);
		ref.setUserId(user.getUserId());
		ref.setOrgRoleId("OrgRoleId" + i);
		ref.setOrgId(org.getOrgId());
		ref.setRoleId(role.getRoleId());
		ref.setOrderNum(i);
		ref.setCreateTime(new Date());
		ref.setCreateUserId("CreateUserId" + i);
		ref.setUpdateTime(new Date());
		ref.setUpdateUserId("UpdateUserId" + i);
		ref.setTenantCode(TenantCode);

		return ref;
	}
}
